//
//  ViesionView.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/3/14.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

///swift新知识 定义一个协议  必须遵守协议 NSObjectProtocol
protocol ViesionViewDelegate: NSObjectProtocol {
    
    ///将要注册
    func registerWillOnClick() ->Void
    ///将要登入
    func loginWillOnClick() -> Void
}

class ViesionView: UIView {

    ///定义一个代理对象
    //定义一个delegate对象 一定要用weak 来修饰  防止强引用造成循环利用
    weak var delegate: ViesionViewDelegate? //只有遵守这个协议的对象才能调用代理方法
    
    let SCREEN_WIDTH = UIScreen.main.bounds.size.width
    let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
    
    /// 设置未登入的界面
    ///
    /// - Parameters:
    ///   - isHome: 是否是首页
    ///   - imageName: 需要显示图片名称
    ///   - title: 需要显示的标题文字
    func stupViesionViewInfo(isHome: Bool, imageName: String, title: String) -> Void {
        iconView.isHidden = !isHome
        homeIconView.image = UIImage.init(named: imageName)
        titleLable.text = title
        //如果是首页才会加这个动画
        if isHome {
            homeStartAnimation()
        }
    }
    ///注册按钮的点击事件
    func registerOnClick(btu: UIButton) -> Void {
        //调用代理注册的登入的按钮
       delegate?.registerWillOnClick()
    }
    ///登入按钮点击事件
    func loginOnClick(btu: UIButton) -> Void {
        delegate?.loginWillOnClick()
    }

    /// 给主页添加动画
    private func homeStartAnimation() -> Void {
        //1.创建动画
        let animation = CABasicAnimation.init(keyPath: "transform.rotation")//旋转动画
        //2.设置动画的属性
        animation.toValue = 2 * M_PI //动画的停止的值
        animation.duration = 20.0//设置动画完成一边的时间
        animation.repeatCount = MAXFLOAT //动画的重复次数
        //当视图的隐藏的时候  动画默认停止  我们要设置属性 让动画不会停止
        animation.isRemovedOnCompletion = false  //默认是true
        //3.添加动画
        iconView.layer.add(animation, forKey: nil)
    }
    
    //View中套添加5个按钮 当你调用init方法的时候 最终也会调用initWithFrame()方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        ///调用添加控件的方法
        initUI()
    }
    //swift建议我们使用如果使用代码 就使用纯代码
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///添加控件
    private func initUI() -> Void {
        
        self.backgroundColor = UIColor.init(colorLiteralRed: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        
        addSubview(titleLable)
        addSubview(registerBtu)
        addSubview(loginBtu)
    
        //要布局  使用autoLayout来布局
        /*
         注意:
         纯代码开发时默认可以直接使用setFrame设置控件位置的
         如果要使用自动布局，需要设置translatesAutoresizingMaskIntoConstraints属性
         一旦加入自动布局系统管理，就不要再设置frame
         */
        
        iconView.frame = CGRect(x: (SCREEN_WIDTH - (iconView.image?.size.width)!)/2, y: (SCREEN_HEIGHT - (iconView.image?.size.height)!)/2 - 20, width: (iconView.image?.size.width)!, height: (iconView.image?.size.height)!)
        
        maskIconView.frame = CGRect(x: (SCREEN_WIDTH - (iconView.image?.size.width)!)/2, y: (SCREEN_HEIGHT - (iconView.image?.size.height)!)/2-30, width: (maskIconView.image?.size.width)!, height: (maskIconView.image?.size.height)!)
        
        homeIconView.frame = CGRect(x: (SCREEN_WIDTH - (homeIconView.image?.size.width)!)/2, y: (SCREEN_HEIGHT - (homeIconView.image?.size.height)!)/2, width: (homeIconView.image?.size.width)!, height: (homeIconView.image?.size.height)!)
        
        //把swift中的转换成oc的NSString 在动态获取的字符串的大小
        let rectString = NSString(cString: titleLable.text!, encoding: String.Encoding.utf8.rawValue)
        let rect = rectString?.boundingRect(with: CGSize.init(width: 200, height: 999), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 14)], context: nil)
        titleLable.frame = CGRect(x: (SCREEN_WIDTH-200)/2, y: homeIconView.frame.origin.y+20+homeIconView.frame.size.height, width: (rect?.size.width)!, height: (rect?.size.height)!)
        
        registerBtu.frame = CGRect(x: 40, y: titleLable.frame.origin.y+(rect?.size.height)!+20, width: (SCREEN_WIDTH-100)/2, height: 30)
        loginBtu.frame = CGRect(x: (SCREEN_WIDTH-100)/2+60, y: titleLable.frame.origin.y+(rect?.size.height)!+20, width: (SCREEN_WIDTH-100)/2, height:30)
    }
    
    
    
    //MARK: - 懒加载所用控件
    
    ///背景图片
    private lazy var iconView = { () -> UIImageView in 
        let icon = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
        return icon
    }()
    
    ///遮罩视图
    private lazy var maskIconView = { () -> UIImageView in
        let imageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    
    /// 小房子
    private lazy var homeIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()

    /// 标签lable
    private lazy var titleLable = { () -> UILabel in
        let lable = UILabel()
        lable.text = "深圳市南三区龙珠三路光前工业园A3栋4楼"
        lable.textColor = UIColor.darkGray
        lable.numberOfLines = 0
        lable.font = UIFont.systemFont(ofSize: 14)
        lable.lineBreakMode = NSLineBreakMode.byWordWrapping
        return lable
    }()
    
    ///注册按钮
    private lazy var registerBtu = { () -> UIButton in
        let btu = UIButton()
        btu.setTitle("注册", for: .normal)
        btu.setTitleColor(UIColor.orange, for: UIControlState.normal)
        btu.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        btu.addTarget(self, action: #selector(registerOnClick(btu:)), for: UIControlEvents.touchUpInside)
        return btu
    }()
    
    ///登入按钮
    private lazy var loginBtu = { () -> UIButton in
        let btu = UIButton()
        btu.setTitle("登入", for: UIControlState.normal)
        btu.setTitleColor(UIColor.orange, for: UIControlState.normal)
        btu.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: UIControlState.normal)
        btu.addTarget(self, action: #selector(loginOnClick(btu:)), for: UIControlEvents.touchUpInside)
        return btu
    }()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
